<?php
	class Sunnet_Plugin_Acl extends Zend_Controller_Plugin_Abstract{
		protected $_auth;
  	 	protected $_acl;
		public function preDispatch(Zend_Controller_Request_Abstract $request){
			if (strtolower($request->getModuleName()) == 'admin'){
				if($_POST['PHPSESSID']){
					Zend_Session::setId($_POST['PHPSESSID']);
				} 
				$auth = Zend_Auth::getInstance();
				$acl = new Sunnet_Acl($auth);
				$this->_acl = $acl;
				$this->_auth = $auth;
				$controller = strtolower($request->getModuleName() . ":" . $request->getControllerName());
				$action = strtolower($request->getActionName());
				$identity = $auth->getIdentity();
				if ($auth->hasIdentity()){
					$role = $identity->username;
				}else{
					$role = 'guest';
				}
				$request = $this->getRequest();
				if ($this->_acl->has($controller)){
					if(!$this->_acl->isAllowed($role,$controller,$action)){
		         		$request->setActionName('error');
		         		$request->setControllerName('error');
		         		$request->setModuleName('admin');
					}
				}
			}
		}
	}

